package com.itstudy.xxx.java8;

import org.junit.Test;

import java.io.Serializable;
import java.time.Year;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * java8练习
 * @author xianpengcheng@aliyun.com
 * @create 2019-05-12 13:26
 * 2.Lambda表达式
 *
 * java8内置的四大核心函数式接口
 *
 * Consumer<T> : 消费型接口  void accept(T t);
 *
 * Supplier<T> : 供给型接口 T get();
 *
 * Function<T,R>: 函数型接口 R apply(T t);
 *
 * Predicate<T> : 断言型接口 boolean test(T t);
 *
 *
 */
public class TestJava8 {

    //员工信息
    List<Employee> emps = Arrays.asList(
            new Employee(101, "张三", 18, 9999.99),
            new Employee(102, "李四", 59, 6666.66),
            new Employee(103, "王五", 28, 3333.33),
            new Employee(104, "赵六", 8, 7777.77),
            new Employee(105, "田七", 38, 5555.55)
    );

    //用lambad完成
    @Test
    public void run1() {
        //需求：获取公司中年龄小于 35 的员工信息
       // List<Employee> collect35 = emps.stream().filter(e -> e.getAge() < 35).collect(Collectors.toList());
        //System.out.println(collect35);
        //需求：获取公司中工资大于 5000 的员工信息
       // List<Employee> employees5000 = emps.stream().filter(employee -> employee.getSalary() > 5000.00).collect(Collectors.toList());
        //System.out.println(employees5000);
        emps.stream().filter(e -> e.getAge() < 35).forEach(System.out::println);
        emps.stream().filter(employee -> employee.getSalary() > 5000.00).forEach(System.out::println);

        //emps.stream().map(Employee::getName).sorted().forEach(System.out::println);
        List<String> list = emps.stream().map(Employee::getName).sorted().limit(3).collect(Collectors.toList());
        list.forEach(System.out::println);

    }

//Runnable开启线程
    @Test
    public void run2() {
        Runnable r1=  () -> {
            System.out.println("线程运行的代码!");
        };
        new Thread(r1).start();


    }


    //自定义函数式接口
    @Test
    public void run3() {
        Consumer s2  = (String name, Integer age) -> {
            System.out.println(name+age);
            return  "hello";
        };
        String aaaa = s2.run("aaaa", 11);
        System.out.println(aaaa);

    }



    //求和
    public  int addUp(Stream<Integer> numbers){
        Integer reduce = numbers.reduce(0, (x, y) -> x + y);
        return reduce;
    }



}
